package com.team622.shop.marketing.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team622.shop.common.exceptions.BusinessException;
import com.team622.shop.common.exceptions.enums.Resp;
import com.team622.shop.marketing.model.entity.Participation;
import com.team622.shop.marketing.service.mapper.ParticipationMapper;
import com.team622.shop.marketing.service.service.ParticipationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author team622
 * @since 2025-07-16
 */
@Service
public class ParticipationServiceImpl extends ServiceImpl<ParticipationMapper, Participation> implements ParticipationService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Participation addParticipation(Participation participation) {
        // 设置默认值
        if (participation.getStatus() == null) {
            participation.setStatus(0); // 默认未完成
        }
        participation.setCreatedAt(new Date());
        participation.setUpdatedAt(new Date());

        this.save(participation);
        return participation;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteParticipation(Long id) {
        if (id == null) {
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Participation updateParticipation(Participation participation) {
        if (participation == null || participation.getId() == null) {
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        participation.setUpdatedAt(new Date());
        this.updateById(participation);
        return participation;
    }

    @Override
    public Participation getParticipationById(Long id) {
        if (id == null) {
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        return this.getById(id);
    }

    @Override
    public Page<Participation> pageParticipation(Integer current, Integer pageSize,
                                               Long activityId, Long memberId, Long couponId,
                                               Integer status) {
        LambdaQueryWrapper<Participation> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(activityId != null, Participation::getActivityId, activityId)
                .eq(memberId != null, Participation::getMemberId, memberId)
                .eq(couponId != null, Participation::getCouponId, couponId)
                .eq(status != null, Participation::getStatus, status);

        // 按创建时间倒序
        queryWrapper.orderByDesc(Participation::getCreatedAt);

        return this.page(new Page<>(current, pageSize), queryWrapper);
    }
}

